package com.tencent.examples.core.processor.common;

import com.alibaba.fastjson.JSONObject;
import com.tencent.bk.utils.blueking.properties.BluekingProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

/**
 * @author :  CHENWEN
 * @Description : 公共参数
 * @Creation Date:  2019/3/28
 */
@Component
public class CommParams {
    @Autowired
    BluekingProperties bluekingProperties;

    /**
     * 白名单登录
     */
    public JSONObject initJson() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("bk_app_code", bluekingProperties.getBk_app_code());
        jsonObject.put("bk_app_secret", bluekingProperties.getBk_app_secret());
        jsonObject.put("bk_username", "admin");

        return jsonObject;
    }

    /**
     * bk_token登录
     * @param request
     * @return
     */
    public JSONObject initJsonOtherMethod(HttpServletRequest request) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("bk_app_code", bluekingProperties.getBk_app_code());
        jsonObject.put("bk_app_secret", bluekingProperties.getBk_app_secret());
//        jsonObject.put("bk_username","admin");
        jsonObject.put("bk_token", getBkTokenByRequest(request));

        return jsonObject;
    }

    public String getBkTokenByRequest(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        String bk_token = "";
        if (cookies.length > 0) {
            for (int i = 0; i < cookies.length; i++) {
                Cookie cook = cookies[i];
                if (cook.getName().equals("bk_token")) { //获取键
                    bk_token = cook.getValue();    //获取值
                }
            }
        }
        return bk_token;
    }
}
